<template>
  <div class="login-scan-container">
    <div class="login-title">
      <span class="login-title-showy">扫码登录</span>
    </div>
    <canvas ref="qrcodeRef"></canvas>
    <div class="font12 mt20 login-msg">
      <i class="iconfont icon-saoyisao mr5"></i>
      <span>打开手机扫一扫，快速登录/注册</span>
    </div>
  </div>
</template>

<script setup lang="ts" name="loginScan">
import { ref, onMounted, nextTick } from 'vue'
import QRCode from "qrcode"; //引入生成二维码插件
import type {QRCodeRenderersOptions} from "qrcode"; //引入生成二维码插件


// 定义变量内容
const qrcodeRef = ref<HTMLCanvasElement | null>(null)

// 初始化生成二维码
const initQrcode = () => {
  nextTick(() => {
    QRCode.toDataURL(`https://qm.qq.com/cgi-bin/qm/qr?k=iNzyqsBsyQKuIxGEznL3PYwLzWOcqmJE&jump_from=webapi`)
    .then(url => {
      console.log(url)
    })
    .catch(err => {
      console.error(err)
    })

    let opts:QRCodeRenderersOptions = {
      errorCorrectionLevel: 'H', //容错级别
      margin: 0,//二维码留白边距
      width: 260,//宽
      color: {
        dark: "#000000",//前景色
        light: "#ffffff"//背景色
      }
    };

    const canvasDom = qrcodeRef.value as HTMLCanvasElement
    const text = `https://qm.qq.com/cgi-bin/qm/qr?k=iNzyqsBsyQKuIxGEznL3PYwLzWOcqmJE&jump_from=webapi` //二维码内容
    // 将获取到的数据（val）画到msg（canvas）上
    QRCode.toCanvas(canvasDom, text, opts, (error: any) => {
        if (error) console.error(error)
        console.log('success!');
    });


  })
}
// 页面加载时
onMounted(() => {
  initQrcode()
})
</script>

<style scoped lang="scss">
.login-title {
  margin-bottom: 50px;
  font-size: 27px;
  text-align: center;
  letter-spacing: 3px;
  color: var(--el-text-color-primary);
  position: relative;
}
.login-scan-animation {
  opacity: 0;
  animation-name: error-num;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
}
.login-scan-container {
  padding: 0 20px 20px;
  display: flex;
  flex-direction: column;
  text-align: center;
  @extend .login-scan-animation;
  animation-delay: 0.1s;
  :deep(canvas) {
    margin: auto;
  }
  .login-msg {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--el-text-color-placeholder);
    @extend .login-scan-animation;
    animation-delay: 0.2s;
  }
}
</style>
